package com.ajk.shop.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ajk.server.exception.ErrorConstant;
import com.ajk.shop.dao.UserAddressMapper;
import com.ajk.shop.exception.ShopServerException;
import com.ajk.shop.model.UserAddress;
import com.ajk.shop.service.UserAddressService;


/**
 * 
 * @author chenxuan
 *
 */
@Service
public class UserAddressServiceImpl implements UserAddressService{

	@Autowired
	private UserAddressMapper userAddressMapper;
	
	@Override
	public UserAddress addUserAddress(UserAddress userAddress) {
		verifyAddress(userAddress);
		if(userAddress.getIsDefault() == 1){
			userAddressMapper.updateUserAddressDefaultToNone(userAddress.getUserId());
		}
		userAddressMapper.addUserAddress(userAddress);
		return userAddress;
	}

	@Override
	public UserAddress modifyUserAddress(UserAddress userAddress) {
		if(userAddress.getId() == null || userAddress.getId() <= 0){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "ID不能为空");
		}
		verifyAddress(userAddress);
		if(userAddress.getIsDefault() == 1){
			userAddressMapper.updateUserAddressDefaultToNone(userAddress.getUserId());
		}
		userAddressMapper.modifyUserAddress(userAddress);
		return userAddress;
	}
	
	
	
	@Override
	public void updateAddressToDefault(UserAddress userAddress) {
		
		userAddressMapper.updateUserAddressDefaultToNone(userAddress.getUserId());
		userAddressMapper.updateUserAddressDefault(userAddress);
	}

	@Override
	public void deleteUserAddress(Integer id) {
		
		userAddressMapper.deleteUserAddress(id);
	}

	@Override
	public List<UserAddress> getUserAddressAll(Integer userId) {
		List<UserAddress> list = userAddressMapper.getUserAddressForAll(userId);
		return list;
	}

	@Override
	public UserAddress getUserAddressById(Integer id) {
		
		return userAddressMapper.getUserAddressById(id);
	}

	@Override
	public UserAddress getUserAddressByDefault(Integer userId) {
		
		return userAddressMapper.getUserAddressDefault(userId);
	}
	
	
	
	private void verifyAddress(UserAddress userAddress){
		if(userAddress.getProvinceId() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "省ID不能为空");
		}
		if(userAddress.getProvince() == null || userAddress.getProvince().isEmpty()){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "省名称不能为空");
		}
		if(userAddress.getCityId() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "城市ID不能为空");
		}
		if(userAddress.getCity() == null || userAddress.getCity().isEmpty()){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "城市名称不能为空");
		}
		if(userAddress.getDistrictId() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "地区ID不能为空");
		}
		if(userAddress.getDistrict() == null || userAddress.getDistrict().isEmpty()){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "地区名称不能为空");
		}
		if(userAddress.getAddress() == null || userAddress.getAddress().isEmpty()){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "详细地址不能为空");
		}
		if(userAddress.getName() == null || userAddress.getName().isEmpty()){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "收件人不能为空");
		}
		if(userAddress.getMobile() == null || userAddress.getMobile().isEmpty()){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "手机号码不能为空");
		}
	}
	

}